import { observable, action, toJS } from 'mobx';
import * as API_ADD from '../apis/add';
import { isEmpty, remove } from 'lodash';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

export class DocAddStore {
  @observable title = getLabel(1986, '新建文档');
  @observable loading = false;
  @observable loadingAdd = false;
  @observable treeDatas = [];
  @observable selectedDomId = [];
  @observable key = [];
  @observable searchValue = '';
  @observable domKey = '';
  @observable linkRout = '';
  @observable linkUrl = '';
  @observable openNewWindow = '';
  @observable isWevarSystem = '0';
  @observable params = '';
  @observable companyDropMenu = [
    {
      companyid: '1',
      name: getLabel(385079, '全部目录'),
    }, {
      companyid: '2',
      name: getLabel(18030, '我的收藏'),
    }, {
      companyid: '3',
      name: getLabel(28183, '常用目录'),
    },
  ];
  @observable expandedKey = [];
  @observable isTreeNodeEmpty = true;
  @observable companysDefault = {
    companyid: '1',
    name: getLabel(385079, '全部目录'),
  }
  @observable isChooseCatalog = false;
  @observable accounts = {
    belong_users: [],
    canCreate: false,
    f_weaver_belongto_userid: '',
    f_weaver_belongto_usertype: '',
  }
  constructor() {
    this.setTitle = this.setTitle.bind(this);
    this.setLoading = this.setLoading.bind(this);
    this.setSelectedDomId = this.setSelectedDomId.bind(this);
    this.clearSelectedDomId = this.clearSelectedDomId.bind(this);
    this.setsearchValue = this.setsearchValue.bind(this);
    this.getTreeDatas = this.getTreeDatas.bind(this);
    this.setExpandedKey = this.setExpandedKey.bind(this);
    this.setKey = this.setKey.bind(this);
    this.setIsTreeNodeEmpty = this.setIsTreeNodeEmpty.bind(this);
    this.setTreeDatas = this.setTreeDatas.bind(this);
    this.getAccount = this.getAccount.bind(this);
  }

  @action
  setTitle(title) {
    this.title = title;
  }

  setIsWevarSystem(isWevarSystem) {
	 this.isWevarSystem = isWevarSystem;
  }
  setLoading(loading) {
    this.loading = loading;
  }
  setSelectedDomId(selectedDomId) {
    this.selectedDomId = [selectedDomId];
    this.setExpandedKeysByChooseId(selectedDomId);
  }
  // 选中id 反向确定展开的id 且刷新右侧标题导航
  setExpandedKeysByChooseId = (id) => {
    const expandedKeys = toJS(this.expandedKey),
_treeDatas = toJS(this.treeDatas);
    if (!_treeDatas.find(o => o.domid === id)) {
       let tempExpandedKey = [];
       _treeDatas.forEach((item) => {
         if (!isEmpty(item.childs)) {
           const tempkey = this.getItem(item, id);
           tempkey.forEach((i) => {
             if (i.getNode) {
               const temptreeData = this.getShowItem(tempkey, i.item.domid, i.item.pid);
              //  this.setTabData(temptreeData);
               temptreeData.forEach(o => tempExpandedKey.push(o.domid));
               remove(tempExpandedKey, o => toJS(expandedKeys).indexOf(o) > -1 || o === id);
               tempExpandedKey = tempExpandedKey.concat(toJS(expandedKeys));
               this.expandedKey = tempExpandedKey;
             }
           });
         }
       });
    }
  }
  getItem = (item, selected) => {
    let getNode = false;
    let returnItem = [];
    if (item.domid === selected) {
      getNode = true;
      returnItem.push({ item, getNode });
    } else {
      returnItem.push({ item, getNode });
      if (item.haschild) {
        item.childs.forEach((i) => {
          let tempResult = [];
          tempResult = this.getItem(i, selected);
          returnItem = returnItem.concat(tempResult);
        });
      }
    }
    return returnItem;
  }
  getShowItem = (tempResult, domid, pid) => {
    let result = [];
    let temp = [];
    tempResult.forEach((item) => {
      if (item.item.domid === domid) {
        result.unshift(item.item);
      }
    });
    tempResult.forEach((item) => {
      if (item.item.key === pid) {
        result.unshift(item.item);
        temp = [];
        if (pid !== '') {
          temp = this.getShowItem(tempResult, item.item.domid, item.item.pid);
          temp.pop();
          result = temp.concat(result);
        }
      }
    });
    return result;
  }
  clearSelectedDomId() {
    this.selectedDomId = [];
    this.expandedKey = [];
  }
  setKey(key) {
    this.key = key;
    this.getAccount({ categoryid: key[0] });
  }
  setsearchValue(searchValue) {
    this.searchValue = searchValue;
  }
  setCompanysDefault(companysDefault = {}) {
    this.companysDefault = companysDefault;
  }
  getTreeDatas(params = { isFav: '0', isCommon: '0', openNewWindow: '1' }) {
    this.selectedDomId = [];
    this.searchValue = '';
    this.loadingAdd = true;
    API_ADD.getTreeDatas(params).then((data) => {
      this.treeDatas = data.treedata;
      this.domKey = data.domKey;
      this.linkRout = data.linkRout;
      this.linkUrl = data.linkUrl;
      this.openNewWindow = data.openNewWindow;
      this.params = data.params;
      this.loadingAdd = false;
	  this.isWevarSystem = data.isWevarSystem;
    });
  }
  setExpandedKey(expandedKey) {
    this.expandedKey = expandedKey;
  }
  setIsTreeNodeEmpty(isTreeNodeEmpty) {
  	this.isTreeNodeEmpty = isTreeNodeEmpty;
  }
  setTreeDatas(treeDatas) {
    this.treeDatas = treeDatas;
  }
  getAccount(params = {}) {
    this.loadingAdd = true;
    API_ADD.getAccount(params).then((data) => {
      this.accounts = data;
      this.loadingAdd = false;
    });
  }
}

